# -*- coding: utf-8 -*-
# @Time    :2022/1/12 22:24
# @AUTHOR  :MORAN
# @Email   :moran@maqu.com
# @File    :serializers.py
# @Software:PyCharm
from rest_framework.serializers import ModelSerializer
from .models import Classes
from users.serializers import UserSerializer
from rest_framework.fields import SerializerMethodField


class ClassesSerializer(ModelSerializer):
    # member = UesrSerializer(many=True)
    memberDetail = SerializerMethodField()

    class Meta:
        model = Classes
        exclude = ['is_delete']

        extra_kwargs = {
            'member': {'write_only': True}  # 只写
        }

    def get_memberDetail(self, classes):
        serializer = UserSerializer(classes.member.all(), many=True)
        data = {
            'teacher': [i for i in serializer.data if i['is_staff'] or 1 in i['groups']],
            'student': [i for i in serializer.data if 2 in i['groups']]
        }
        return data
